package fhp.hlhj.giantfold.activity.malls

import android.content.Intent
import android.support.v7.widget.GridLayoutManager
import android.view.LayoutInflater
import android.view.View
import com.alibaba.baichuan.android.trade.AlibcTrade
import com.alibaba.baichuan.android.trade.callback.AlibcTradeCallback
import com.alibaba.baichuan.android.trade.model.AlibcShowParams
import com.alibaba.baichuan.android.trade.model.OpenType
import com.alibaba.baichuan.android.trade.page.AlibcDetailPage
import com.alibaba.baichuan.trade.biz.context.AlibcTradeResult
import com.alibaba.baichuan.trade.biz.core.taoke.AlibcTaokeParams
import com.chad.library.adapter.base.BaseQuickAdapter
import com.kepler.jd.login.KeplerApiManager
import com.kepler.jd.sdk.bean.KeplerAttachParameter
import com.lzy.okgo.model.HttpParams
import fhp.hlhj.giantfold.BaseActivity
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.adapter.MallAapter
import fhp.hlhj.giantfold.adapter.MallHeaderGridAdp
import fhp.hlhj.giantfold.interfaces.ICreateOrder
import fhp.hlhj.giantfold.interfaces.IMall
import fhp.hlhj.giantfold.javaBean.BaseBean
import fhp.hlhj.giantfold.javaBean.GoodListBean
import fhp.hlhj.giantfold.javaBean.MallBean
import fhp.hlhj.giantfold.presenter.OrderPresenter
import fhp.hlhj.giantfold.presenter.MallPresenter
import fhp.hlhj.giantfold.utils.LogUtil
import kotlinx.android.synthetic.main.activity_day_red.*
import kotlinx.android.synthetic.main.activity_mall_aty.*
import kotlinx.android.synthetic.main.tabbao_header.view.*

class MallAty : BaseActivity(),IMall, ICreateOrder {
    private lateinit var mallAdp:MallAapter
    private lateinit var headerAdp:MallHeaderGridAdp
    private lateinit var header:View
    private lateinit var presenter:MallPresenter
    private lateinit var createOderPresenter: OrderPresenter
    private val itemDatas=ArrayList<GoodListBean.DataBean>()
    private val headerDatas=ArrayList<MallBean.DataBean>()
    private var goodType=0
    override fun getContentId(): Int = R.layout.activity_mall_aty

    override fun initView() {
        presenter= MallPresenter(this)
        val gridManager=GridLayoutManager(this,2)
        mallAdp= MallAapter(R.layout.tabbao_item,itemDatas)
        recyclerview.layoutManager=gridManager
        recyclerview.adapter=mallAdp
        /*header设置*/
        header = LayoutInflater.from(this).inflate(R.layout.tabbao_header,null)
        header.btExit.setOnClickListener {
            finish()
        }
        when(intent.getStringExtra("name")){
            "淘宝"->{
                /*header.imgTittle.setImageResource(R.mipmap.taobao_icon)
                header.loback.setBackgroundResource(R.drawable.jingdong_back)
                header.btSearch.setBackgroundResource(R.drawable.search_btback)*/
                goodType=0
            }
            "天猫"->{
                goodType=1
                header.imgTittle.setImageResource(R.mipmap.tianmaowang_tmlogo)
                header.loback.setBackgroundResource(R.drawable.jingdong_back)
                header.btSearch.setBackgroundResource(R.drawable.search_btback_jingdong)
            }
            "京东"->{
                header.imgTittle.setImageResource(R.mipmap.jingdong_jdlogo)
                header.loback.setBackgroundResource(R.drawable.jingdong_back)
                header.btSearch.setBackgroundResource(R.drawable.search_btback_jingdong)
            }
        }
        header.btSS.setOnClickListener {
            startActivity(Intent(this,SearchGoodAty::class.java))
        }
        headerAdp= MallHeaderGridAdp(this,headerDatas)
        header.gridView.adapter=headerAdp
        mallAdp.addHeaderView(header)
        val params = HttpParams()
        params.put("parent_id",intent.getStringExtra("pid"))
        presenter.getMall(params)
        mallAdp.onItemClickListener = BaseQuickAdapter.OnItemClickListener { adapter, view, position ->
            if (itemDatas[position].b_type.equals("taobao")){
                val showParams = AlibcShowParams(OpenType.H5, false)
                val detailPage = AlibcDetailPage(itemDatas[position].product_id)
                val params = AlibcTaokeParams()
                params.pid="mm_116147303_36832537_132678582"
                AlibcTrade.show(this@MallAty,detailPage,showParams,params,null,object :AlibcTradeCallback{
                    override fun onFailure(p0: Int, p1: String?) {
                        LogUtil.log("失败$p1")
                    }
                    override fun onTradeSuccess(p0: AlibcTradeResult) {
                        LogUtil.log("成功${p0.payResult.paySuccessOrders}+${p0.resultType.name}Pid为mm_116147303_36832537_132678582")
                        if (null!=p0.payResult.paySuccessOrders&&p0.payResult.paySuccessOrders.size>0){
                            val intent = Intent(this@MallAty, TBOrderListAty::class.java)
                            intent.putExtra("pid",itemDatas[position].product_id)
                            intent.putExtra("et",1)
                            startActivity(intent)
                        }
                    }
                })
            }else if(itemDatas[position].big_type.equals("jd")){
                val parameter = KeplerAttachParameter()
                parameter.setCustomerInfo("hlhjjz")
                KeplerApiManager.getWebViewService().openItemDetailsWebViewPage(itemDatas[position].product_id,parameter)
            }

        }
        /*获取商品条目*/
        val goodsP = HttpParams()
        goodsP.put("product_belong_to",goodType)
        presenter.getGoods(goodsP)

        presenter.setItemclick()
    }
    override fun showLoading() {
        loadingDialog.show()
    }

    override fun getMall(mallBean: MallBean) {
        loadingDialog.dismiss()
        headerDatas.addAll(mallBean.data)
        headerAdp.notifyDataSetChanged()
    }

    /**
     * 获取商品列表
     */
    override fun getGoods(goodListBean: GoodListBean) {
        itemDatas.addAll(goodListBean.data)
        mallAdp.notifyDataSetChanged()
    }
    override fun setItemClick() {
        LogUtil.log("设置点击")

    }

    override fun changeOrder(baseBean: BaseBean) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    /**
     * 创建订单
     */
    override fun createOrder(baseBean: BaseBean) {
        if (baseBean.code==200){
            LogUtil.log("创建订单成功")
        }
    }


}
